package com.vernhe.circle;

public class CircleDeque<E> {
    //循环双端队列不需要加rear这变量，因为rear = front + length
    private int size;
    private E[] elements;
    private int front;  //默认0
    private static final int DEFAULT_CAPACITY = 10;

    public CircleDeque() {
        elements = (E[]) new Object[DEFAULT_CAPACITY];
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public void clear() {
        //如果使用i<elements.length的话会有些浪费性能了
        for (int i = 0; i < size; i++) {
            elements[index(i)] = null;
        }
        size = 0;
        front = 0;
    }

    /**
     * 从头部入队
     * @param element
     */
    public void enQueueFront(E element) {
        ensureCapacity(size + 1);
        front = index(-1);
        elements[front] = element;
        size++;
    }

    /**
     * 从尾部入队
     * @param element
     */
    public void enQueueRear(E element) {
        ensureCapacity(size + 1);
        elements[index(size)] = element;
        size++;
    }

    /**
     * 从头部出队
     * @return
     */
    public E deQueueFront() {
        E frontElement = elements[front];
        elements[front] = null;
        front = index(1);
        size--;
        return frontElement;
    }

    /**
     * 从尾部出队
     * @return
     */
    public E deQueueRear() {
        int rearIndex = index(size - 1);
        E rearElement = elements[rearIndex];
        elements[rearIndex] = null;
        size--;
        return rearElement;
    }

    public E front() {
        return elements[front];
    }

    public E rear() {
        return elements[index(size - 1)];
    }

    /**
     * 获取真实索引，逻辑上始终认为front=0
     * @param index 逻辑下标
     * @return
     */
    private int index(int index) {
        index += front;
        if (index < 0) {
            //若长度为7 当front=0时，往前添加元素，index(-1)=6
            return index + elements.length;
        }
        //优化取模运算，m % n = m - (m >= n ? n : 0)前提条件是m < 2n
        //return index % elements.length;
        return index - (index >= elements.length? elements.length : 0);
    }

    private void ensureCapacity(int capacity) {
        int oldCapcity = elements.length;
        //容量够用
        if (oldCapcity >= capacity) return;
        //扩容为旧容量的1.5倍
        int newCapacity = oldCapcity + (oldCapcity >> 1);
        E[] newElements = (E[]) new Object[newCapacity];
        for (int i = 0; i < size; i++) {
            //因为循环队列并不是从0下标开始的
            newElements[i] = elements[index(i)];
        }
        front = 0;
        elements = newElements;
    }

    @Override
    public String toString() {
        StringBuilder string = new StringBuilder();
        string.append("capacity=").append(elements.length).
                append("front=").append(front).
                append("size=" + size).append(",[");
        for (int i = 0; i < elements.length; i++) {
            if (i != 0) {
                string.append(",");
            }
            string.append(elements[i]);
        }
        string.append("]");
        return string.toString();
    }
}
